/**
 * @program: LeetCode
 * @description: LeetCode : 二叉搜索树中找P和Q的最小公共祖先
 * @author: WXY
 * @create: 2022-12-11 16:21
 * @Version 1.0
 **/


public class Num068_1_lowestCommonAncestor {
    public static TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
        if (root == null) return null;
        if (root.val < p.val && root.val < q.val) {
            return lowestCommonAncestor(root.right, p, q);
        } else if (root.val > p.val && root.val > q.val) {
            return lowestCommonAncestor(root.left, p, q);
        } else {
            return root;
        }

    }

    public class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        TreeNode(int x) {
            val = x;
        }
    }
}
